// items.xml parser

function Item(AItemElement) {
  // parse single item
  var that = new Object();
  that.id = AItemElement.getAttribute('id');
  that.type = AItemElement.getAttribute('type');
  that.name = AItemElement.getAttribute('name');
  that.image = AItemElement.getAttribute('image');
  that.description = AItemElement.getAttribute('description');
  that.effect = AItemElement.getAttribute('effect');
  that.type = AItemElement.getAttribute('type');
  that.hp = AItemElement.getAttribute('hp');
  that.weight = AItemElement.getAttribute('weight');
  that.max_per_slot = AItemElement.getAttribute('max_per_slot');
  that.weight = AItemElement.getAttribute('weight');
  that.weapon_type = AItemElement.getAttribute('weapon-type');
  that.view = AItemElement.getAttribute('view');
  that.defense = AItemElement.getAttribute('defense');
  that.attack_action = AItemElement.getAttribute('attack_action');
  that.attack_range = AItemElement.getAttribute('attack_range');
  var xml_sprite = AItemElement.getElementsByTagName('sprite');
  
  if (that.id == -100) {
    debug('-100: '+xml_sprite.length);
  }
    
  that.sprite = new Array();
  for (var s=0; s<xml_sprite.length; s++) {
    that.sprite[s] = new Object();
    if (xml_sprite[s].hasAttribute("gender"))
      that.sprite[s].gender = xml_sprite[s].getAttribute('gender');
    else
      that.sprite[s].gender = 'any';
    that.sprite[s].xml = xml_sprite[s].firstChild.nodeValue;
  }

  if (that.id == -100) {
    debug('-100: '+xml_sprite[0].firstChild.nodeValue);
  }

  
  that.debug = function() {
    debug('Item: id='+this.id+' type="'+this.type+'" name="'+this.name+'" sprite[0]='+this.sprite[0]);
  }
  
  return that;
}

function Items(AXmlFile) {
  // parse items.xml
  debug('Parsing "'+AXmlFile+'"');
  xml = sjax(AXmlFile);
  xml.documentElement.normalize();
  xml_item = xml.documentElement.getElementsByTagName('item');
  debug('  '+xml_item.length+' items found');

  this.item = new Array();
  for (var i=0; i<xml_item.length; i++) {
    this.item[i] = new Item(xml_item[i]);
    //this.item[i].debug();
  }
  debug('  '+this.item.length+' items parsed');

  this.sqlSprites = function() {
    // create sql alter for sprites only
    debug('  // sprites');
//    debug('  // 2 - hairstyle');
    for (var i=0; i<this.item.length; i++)
//      if (this.item[i].type == "hairsprite")
        for (var s=0; s<this.item[i].sprite.length; s++)
          debug('  puInsert("insert into spri values (0, '+this.item[i].id+', \''+this.item[i].sprite[s].gender+'\', \''+this.item[i].sprite[s].xml.split('|')[0]+'\')");');
    debug('');
    
  }
  
  this.sql = function() {
    // create sql alter for known items
    debug('  // SQL alter for eqty table');
    debug('');
    debug('  // 1 - head');
    for (var i=0; i<this.item.length; i++)
      if (this.item[i].type == "equip-head")
        debug('  puInsert("insert into eqty values ('+this.item[i].id+', 1, \''+this.item[i].sprite[0].split('|')[0].replace('-male','-RACE')+'\', \''+this.item[i].name.replace("'","\\'")+'\')");');
    debug('');
    debug('  // 2 - hairstyle');
    for (var i=0; i<this.item.length; i++)
      if (this.item[i].type == "hairsprite")
        debug('  puInsert("insert into eqty values ('+this.item[i].id+', 2, \''+this.item[i].sprite[0].split('|')[0].replace('-male','-RACE')+'\', \''+this.item[i].name.replace("'","\\'")+'\')");');
    debug('');
    debug('  // 3 - chest');
    for (var i=0; i<this.item.length; i++)
      if (this.item[i].type == "equip-torso")
        debug('  puInsert("insert into eqty values ('+this.item[i].id+', 3, \''+this.item[i].sprite[0].split('|')[0].replace('-male','-RACE')+'\', \''+this.item[i].name.replace("'","\\'")+'\')");');
    debug('');
    debug('  // 4 - leg');
    for (var i=0; i<this.item.length; i++)
      if (this.item[i].type == "equip-legs")
        debug('  puInsert("insert into eqty values ('+this.item[i].id+', 4, \''+this.item[i].sprite[0].split('|')[0].replace('-male','-RACE')+'\', \''+this.item[i].name.replace("'","\\'")+'\')");');
    debug('');
    debug('  // 5 - feet');
    for (var i=0; i<this.item.length; i++)
      if (this.item[i].type == "equip-feet")
        debug('  puInsert("insert into eqty values ('+this.item[i].id+', 5, \''+this.item[i].sprite[0].split('|')[0].replace('-male','-RACE')+'\', \''+this.item[i].name.replace("'","\\'")+'\')");');
    debug('');
    debug('  // 6 - hands');
    for (var i=0; i<this.item.length; i++)
      if (this.item[i].type == "equip-arms")
        try {
          debug('  puInsert("insert into eqty values ('+this.item[i].id+', 6, \''+this.item[i].sprite[0].split('|')[0].replace('-male','-RACE')+'\', \''+this.item[i].name.replace("'","\\'")+'\')");');
        } catch(e) {
          debug('  ... error: '+e+', name='+this.item[i].name);
        }

    debug('');
    debug('  // 7 - race');
    for (var i=0; i<this.item.length; i++)
      if (this.item[i].type == "racesprite")
        debug('  puInsert("insert into eqty values ('+this.item[i].id+', 7, \''+this.item[i].sprite[0].split('|')[0].replace('-male','-RACE')+'\', \''+this.item[i].name.replace("'","\\'")+'\')");');
  }
}

// previous was sever items, this is client items

var items = new Array();

function ItemsSelects() {
  // request xtr from server/items.php, parse it and create select elements
  ///debug('Requesting items...');
  var xtr = new PuXtr('server/items.php','');
  if (xtr.row.length <= 0)
    throw "ItemSelects received empty xml: "+sta[0]+' - '+sta[1];
  ///debug(puXtrStatus(xtr)+' - '+row.length+' rows');
  // get selects
  var selHead1 = document.getElementById('selHead1');
  var selHairstyle1 = document.getElementById('selHairstyle1');
  var selChest1 = document.getElementById('selChest1');
  var selLeg1 = document.getElementById('selLeg1');
  var selFeet1 = document.getElementById('selFeet1');
  var selHands1 = document.getElementById('selHands1');
  //var selRace1 = document.getElementById('selRace1');
  // first item empty or none
  selHead1.innerHTML += "<option value=\"0\"></option>";
  selHairstyle1.innerHTML += "<option value=\"0\"></option>";
  selChest1.innerHTML += "<option value=\"0\"></option>";
  selLeg1.innerHTML += "<option value=\"0\"></option>";
  selFeet1.innerHTML += "<option value=\"0\"></option>";
  selHands1.innerHTML += "<option value=\"0\"></option>";
  //selRace1.innerHTML += "<option value=\"0\"></option>";
  // parse all rows
  for (var i=0; i<xtr.row.length; i++) {
    // <row i_eqty="511" i_eqlo="1" v_eqlo="Head" v_sprite="head-santahat.xml" v_name="Santa Hat" />
    var i_eqty = xtr.row[i].getAttribute('i_eqty');
    var i_eqlo = xtr.row[i].getAttribute('i_eqlo');
    var v_eqlo = xtr.row[i].getAttribute('v_eqlo');
    var v_sprite = xtr.row[i].getAttribute('v_sprite');
    var v_name = xtr.row[i].getAttribute('v_name');
    var i_red = xtr.row[i].getAttribute('i_red');
    var i_green = xtr.row[i].getAttribute('i_green');
    var i_blue = xtr.row[i].getAttribute('i_blue');
    // try to load the sprite
    items[i_eqty] = new Sprite('graphics/sprites/'+v_sprite,i_red,i_green,i_blue);
    items[i_eqty].i_eqty = i_eqty;
    // head
    if (v_eqlo == 'Head')      selHead1.innerHTML += "<option value=\""+i_eqty+"\">"+v_name+"</option>";
    if (v_eqlo == 'Hairstyle') selHairstyle1.innerHTML += "<option value=\""+i_eqty+"\">"+v_name+"</option>";
    if (v_eqlo == 'Chest')     selChest1.innerHTML += "<option value=\""+i_eqty+"\">"+v_name+"</option>";
    if (v_eqlo == 'Leg')       selLeg1.innerHTML += "<option value=\""+i_eqty+"\">"+v_name+"</option>";
    if (v_eqlo == 'Feet')      selFeet1.innerHTML += "<option value=\""+i_eqty+"\">"+v_name+"</option>";
    if (v_eqlo == 'Hands')     selHands1.innerHTML += "<option value=\""+i_eqty+"\">"+v_name+"</option>";
    //if (v_eqlo == 'Race')      selRace1.innerHTML += "<option value=\""+i_eqty+"\">"+v_name+"</option>";
  }
  
  // lookup for sprites ???
  this.lookup = function(AIEqty) {
    for (var i=0; i<xtr.row.length; i++)
      if (this[i].i_eqty == AIEqty) {
        return this[i];
        break;
      }
    return null;
  }
}

